package cn.u313.lymusic.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.media.MediaScannerConnection
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.u313.lymusic.R
import cn.u313.lymusic.adapter.MultiDownloadAdapter
import com.arialyy.aria.core.Aria
import com.arialyy.aria.core.download.DownloadEntity
import com.arialyy.aria.core.download.DownloadTaskListener
import com.arialyy.aria.core.task.DownloadTask


class MultiDownloadActivity : AppCompatActivity(), DownloadTaskListener {
    private var mData = ArrayList<DownloadEntity>()
    private lateinit var mAdapter: MultiDownloadAdapter

    //    override fun getViewBinding(): ActivityMultiDownloadBinding {
//        return ActivityMultiDownloadBinding.inflate(layoutInflater)
//    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_download_new);
        initView()
    }
    lateinit var  rvDownloadList: RecyclerView
    lateinit var tvDownload: TextView
    private fun initView() {
        rvDownloadList = findViewById<RecyclerView>(R.id.rv_download_list)
        tvDownload = findViewById<TextView>(R.id.tv_download)
        Aria.download(this).register();
        title = "下载列表"
        val temps = Aria.download(this).allNotCompleteTask
        if (temps != null && temps.isNotEmpty()) {
            mData.addAll(temps as ArrayList<DownloadEntity>)
        }
        mAdapter = MultiDownloadAdapter(this, mData)
        mAdapter.init()
        rvDownloadList?.layoutManager = LinearLayoutManager(this)
        rvDownloadList?.adapter = mAdapter
        findViewById<View>(R.id.download_record).setOnClickListener {
            startActivity(Intent(this,SearchActivity::class.java))
        }
        findViewById<View>(R.id.download_record1).setOnClickListener {
            startActivity(Intent(this,DlJiluActivity::class.java))
        }
        initData()
    }

    @SuppressLint("SetTextI18n")
     fun initData() {
        if (mData.size == 0) {
            tvDownload.visibility = View.VISIBLE
            rvDownloadList.visibility = View.GONE
        } else {
            tvDownload.visibility = View.GONE
            rvDownloadList.visibility = View.VISIBLE
        }
    }

    override fun onWait(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
        Log.d(TAG, task.taskName + ", " + task.state)
    }
    val TAG = "下载界面"
    override fun onPre(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
        Log.d(TAG, task.taskName + ", " + task.state)
    }

    override fun onTaskPre(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
        Log.d(TAG, task.taskName + ", " + task.state)
    }

    override fun onTaskResume(task: DownloadTask?) {
        if (task ==null)return
        Log.d(TAG, task!!.taskName + ", " + task.state)
        mAdapter.updateState(task.entity)
    }

    override fun onTaskStart(task: DownloadTask?) {
        if (task ==null)return
        Log.d(TAG, task!!.taskName + ", " + task.state)
        mAdapter.updateState(task.entity)
    }

    override fun onTaskStop(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
    }

    override fun onTaskCancel(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
    }

    override fun onTaskFail(task: DownloadTask?, e: Exception?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
    }

    override fun onTaskComplete(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
        try {
            MediaScannerConnection.scanFile(
                this,
                arrayOf<String>(task.filePath),
                null
            ) { path, uri ->
                // 扫描完成后的回调处理
            }
        }catch (e:Exception){
            e.printStackTrace()
        }
    }

    override fun onTaskRunning(task: DownloadTask) {
        if (task ==null)return
//        log(task!!.entity.toString())
        mAdapter.setProgress(task.entity)
    }

    override fun onNoSupportBreakPoint(task: DownloadTask?) {

    }

    override fun onResume() {
        super.onResume()
        val temps = Aria.download(this).allNotCompleteTask
        if (temps != null && temps.isNotEmpty()) {
            mData.clear()
            mData.addAll(temps as ArrayList<DownloadEntity>)
            mAdapter.notifyDataSetChanged()
            Log.e(TAG, "onResume: "+mData.size )
            mAdapter.init()
        }

        initData()
    }
}